package cn.jhz.learn.blog.controller.core.auth;

import cn.jhz.learn.blog.common.bean.JsonData;
import cn.jhz.learn.blog.component.GLOBAL;
import cn.jhz.learn.blog.component.shiro.PermissionName;
import cn.jhz.learn.blog.service.core.auth.article.ConsoleArticleService;
import cn.jhz.learn.blog.util.JwtUtil;
import cn.jhz.learn.blog.vo.article.query.page.ConsolePageView;
import cn.jhz.learn.blog.vo.article.submit.ArticleParam;

import com.google.common.collect.Maps;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jose4j.jwt.MalformedClaimException;
import org.jose4j.jwt.consumer.InvalidJwtException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.Map;


@RestController
@RequestMapping("/api/auth/core/article/")
public class ConsoleArticleController {

    private final ConsoleArticleService articleService;

    @Autowired
    public ConsoleArticleController(ConsoleArticleService articleService) {
        this.articleService = articleService;
    }

    @PostMapping("create")
    @RequiresPermissions("article:create")
    @PermissionName("createArticle")
    public JsonData createArticle(@RequestHeader(value = GLOBAL.TOKEN_INFO.AUTH_HEADER)String jwt,
                                  @Validated @NotNull @RequestBody ArticleParam article) throws MalformedClaimException, InvalidJwtException {
        Map<String, Object> result = Maps.newHashMap();
        result.put("articleId", articleService.create(JwtUtil.getInstance().getUid(jwt), article));
        return JsonData.success(result);
    }

    @DeleteMapping("{article_id}/del")
    @RequiresPermissions("article:delete")
    @PermissionName("deleteArticle")
    public JsonData deleteArticle(@RequestHeader(value = GLOBAL.TOKEN_INFO.AUTH_HEADER)String jwt,
                                  @PathVariable("article_id") Integer articleId,
                                  @Validated @NotNull @RequestBody Map<String, Boolean> params) throws MalformedClaimException,
            InvalidJwtException {
        articleService.delete(JwtUtil.getInstance().getUid(jwt), articleId, params.get("deep"));
        return JsonData.success();
    }

    @PutMapping("put")
    @RequiresPermissions("article:put")
    @PermissionName("putArticle")
    public JsonData updateArticle(@RequestHeader(value = GLOBAL.TOKEN_INFO.AUTH_HEADER)String jwt,
                                   @RequestBody @NotNull @Validated ArticleParam article) throws MalformedClaimException, InvalidJwtException {
        articleService.update(JwtUtil.getInstance().getUid(jwt), article);
        return JsonData.success();
    }

    @PatchMapping("{article_id}/stick")
    @RequiresPermissions("article:patch")
    @PermissionName("stickArticle")
    public JsonData stickArticle(@RequestHeader(value = GLOBAL.TOKEN_INFO.AUTH_HEADER)String jwt,
                                 @PathVariable("article_id") Integer articleId,
                                 @Validated @NotNull @RequestBody Map<String, Boolean> params) throws MalformedClaimException,
            InvalidJwtException {

        articleService.stick(JwtUtil.getInstance().getUid(jwt), articleId, params.get("stick"));

        return JsonData.success();
    }

    @PatchMapping("{article_id}/rec")
    @RequiresPermissions("article:patch")
    @PermissionName("recycleArticle")
    public JsonData recycleArticle(@RequestHeader(value = GLOBAL.TOKEN_INFO.AUTH_HEADER)String jwt,
                                   @PathVariable("article_id") Integer articleId) throws MalformedClaimException, InvalidJwtException {
        articleService.recycle(JwtUtil.getInstance().getUid(jwt), articleId);
        return JsonData.success();
    }

    @GetMapping("count")
    @RequiresPermissions("article:get")
    @PermissionName("getCountAndArticle")
    public JsonData getCountAndArticle(@RequestHeader(value = GLOBAL.TOKEN_INFO.AUTH_HEADER)String jwt,
                                       @Validated @NotNull ConsolePageView pageView) throws MalformedClaimException, InvalidJwtException {
        return JsonData.success(articleService.getCountAndList(JwtUtil.getInstance().getUid(jwt), pageView));
    }

}
